package com.mmall.Concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedExample2 {
    //修饰一个类
    public static void test1(int j){
        synchronized (SynchronizedExample2.class){
            for (int i = 0; i < 10; i++) {
                log.info("test1  {}-{}",j ,i);
            }
        }
    }
    //修饰一个方法,该类的所有对象，同一时间只有一个线程可以执行
    public  static void test2(int j){
        for (int i = 0; i < 10; i++) {
            log.info("test2  {}-{}",j ,i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample2 example1 = new SynchronizedExample2();
        SynchronizedExample2 example2 = new SynchronizedExample2(); //不同对象调用不一样
        ExecutorService service = Executors.newCachedThreadPool();
        //线程池的两次调用
        service.execute(()->{
            example1.test1(4);
        });

        service.execute(()->{
            example2.test1(5);
        });
    }
}

